package dfbz.com.dao;

import dfbz.com.dao.base.BaseDao;
import dfbz.com.model.User;
import dfbz.com.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static dfbz.com.constant.Const.MAX_PAGE_SIZE;

public class UserDao extends BaseDao<User> {


    /**
     * 登录的sql（业务sql，不属于基本sql 增删改查，直接用BaseDao）
     *
     * @param username 用户名
     * @return User对象
     */
    public User findUserByName(String username) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        User user = null;
        try {
            user = qr.query("select * from user where username = ? limit 1", new BeanHandler<>(User.class), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public User findUserEmail(String email) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        User user = null;
        try {
            user = qr.query("select * from user where email = ? limit 1", new BeanHandler<>(User.class), email);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public String findDept(String username) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = new HashMap<>();
        try {
            String sql="SELECT d.name \n" +
                    "FROM user u,dept d\n" +
                    "where u.dept_id=d.id and u.username=?";
            map = qr.query(sql, new MapHandler(), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (map != null) {
            return map.get("name").toString();
        }
        return null;
    }
    public List<Map<String,Object>> search(String search ,int page){
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = new HashMap<>();
        String sql="";

        try {
            sql = sql + "select * from user_info where real_name like ? limit ?,?";
            search = search + "%";
            // 将这里的结果因该是List,因为在user.jsp页面是需要userinfoList是List,后面的你自己在该改？？

            List<Map<String, Object>> list = qr.query(sql, new MapListHandler(), search,(page-1)*MAX_PAGE_SIZE,page*MAX_PAGE_SIZE);
            return list;
            //map = qr.query(sql, new MapHandler(),search);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public List<Map<String,Object>> searchTotal(String search){
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = new HashMap<>();
        String sql="";

        try {
            sql = sql + "select * from user_info where real_name like ? ";
            search = search + "%";
            // 将这里的结果因该是List,因为在user.jsp页面是需要userinfoList是List,后面的你自己在该改？？

            List<Map<String, Object>> list = qr.query(sql, new MapListHandler(), search);
            return list;
            //map = qr.query(sql, new MapHandler(),search);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}
